﻿namespace Dcms.DAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;

    public class Dcms_PermissionDAL : IDcms_PermissionDAL
    {
        public IList<Dcms_PermissionModel> Modellist = new List<Dcms_PermissionModel>();

        public Dcms_PermissionModel AddModel(Dcms_PermissionModel model)
        {
            string sQLString = "insert into Dcms_Permission(Permission_Name,Permission_Flag,Permission_ParentID,Permission_Order,Permission_Url,Permission_AllAction,Permission_Layer,Permission_ModelKeyId) values(@Permission_Name,@Permission_Flag,@Permission_ParentID,@Permission_Order,@Permission_Url,@Permission_AllAction,@Permission_Layer,@Permission_ModelKeyId) SELECT @NewId=@@Identity";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@NewId", SqlDbType.Int), new SqlParameter("@Permission_Name", SqlDbType.VarChar), new SqlParameter("@Permission_Flag", SqlDbType.VarChar), new SqlParameter("@Permission_ParentID", SqlDbType.Int), new SqlParameter("@Permission_Order", SqlDbType.Int), new SqlParameter("@Permission_Url", SqlDbType.VarChar), new SqlParameter("@Permission_AllAction", SqlDbType.VarChar), new SqlParameter("@Permission_Layer", SqlDbType.Int), new SqlParameter("@Permission_ModelKeyId", SqlDbType.SmallInt) };
            cmdParms[0].Direction = ParameterDirection.Output;
            cmdParms[1].Value = model.Permission_Name;
            cmdParms[2].Value = model.Permission_Flag;
            cmdParms[3].Value = model.Permission_ParentID;
            cmdParms[4].Value = model.Permission_Order;
            cmdParms[5].Value = model.Permission_Url;
            cmdParms[6].Value = model.Permission_AllAction;
            cmdParms[7].Value = model.Permission_Layer;
            cmdParms[8].Value = model.Permission_ModelKeyId;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            model.Permission_Id = (int) cmdParms[0].Value;
            return model;
        }

        public IList<Dcms_PermissionModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_PermissionModel> list = new List<Dcms_PermissionModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_PermissionModel item = new Dcms_PermissionModel();
                if (ds.Tables[0].Rows[i]["Permission_Id"].ToString() != "") item.Permission_Id = int.Parse(ds.Tables[0].Rows[i]["Permission_Id"].ToString());
                item.Permission_Name = ds.Tables[0].Rows[i]["Permission_Name"].ToString();
                item.Permission_Flag = ds.Tables[0].Rows[i]["Permission_Flag"].ToString();
                if (ds.Tables[0].Rows[i]["Permission_ParentID"].ToString() != "") item.Permission_ParentID = int.Parse(ds.Tables[0].Rows[i]["Permission_ParentID"].ToString());
                if (ds.Tables[0].Rows[i]["Permission_Order"].ToString() != "") item.Permission_Order = int.Parse(ds.Tables[0].Rows[i]["Permission_Order"].ToString());
                item.Permission_Url = ds.Tables[0].Rows[i]["Permission_Url"].ToString();
                item.Permission_AllAction = ds.Tables[0].Rows[i]["Permission_AllAction"].ToString();
                if (ds.Tables[0].Rows[i]["Permission_Layer"].ToString() != "") item.Permission_Layer = int.Parse(ds.Tables[0].Rows[i]["Permission_Layer"].ToString());
                if (ds.Tables[0].Rows[i]["Permission_ModelKeyId"].ToString() != "") item.Permission_ModelKeyId = int.Parse(ds.Tables[0].Rows[i]["Permission_ModelKeyId"].ToString());
                list.Add(item);
            }
            return list;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_Permission where Permission_Id=@Permission_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Permission_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return SqlHelper.GetNoteCount(SqlStr).ToString();
        }

        public IList<Dcms_PermissionModel> GetDataListByPage()
        {
            return this.DataSetToList(SqlHelper.CreateDataSet("Select * from Dcms_Permission Where Permission_ParentID=0 Order By Permission_Id Desc"));
        }

        public IList<Dcms_PermissionModel> GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return this.DataSetToList(SqlHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize));
        }

        public int GetMaxModelKeyID(string strPermission_Flag)
        {
            int noteCount = -1;
            string sql = "select Max(Permission_ModelKeyID) from Dcms_Permission where Permission_Flag='" + strPermission_Flag + "'";
            try
            {
                noteCount = SqlHelper.GetNoteCount(sql);
            }
            catch
            {
            }
            return noteCount;
        }

        public Dcms_PermissionModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_Permission where Permission_Id=@Permission_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Permission_Id", SqlDbType.Int) };
            cmdParms[0].Value = ID;
            Dcms_PermissionModel model = new Dcms_PermissionModel();
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_PermissionModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public IList<Dcms_PermissionModel> GetModelList(int Id)
        {
            string sQLString = "select * from Dcms_Permission where Permission_ParentID=@Permission_ParentID";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Permission_ParentID", SqlDbType.Int) };
            cmdParms[0].Value = Id;
            DataSet set = SqlHelper.CreateDataSet(sQLString, cmdParms);
            for (int i = 0; i < set.Tables[0].Rows.Count; i++)
            {
                Dcms_PermissionModel item = new Dcms_PermissionModel();
                if (set.Tables[0].Rows[i]["Permission_Id"].ToString() != "") item.Permission_Id = int.Parse(set.Tables[0].Rows[i]["Permission_Id"].ToString());
                item.Permission_Name = set.Tables[0].Rows[i]["Permission_Name"].ToString();
                item.Permission_Flag = set.Tables[0].Rows[i]["Permission_Flag"].ToString();
                if (set.Tables[0].Rows[i]["Permission_ParentID"].ToString() != "") item.Permission_ParentID = int.Parse(set.Tables[0].Rows[i]["Permission_ParentID"].ToString());
                if (set.Tables[0].Rows[i]["Permission_Order"].ToString() != "") item.Permission_Order = int.Parse(set.Tables[0].Rows[i]["Permission_Order"].ToString());
                item.Permission_Url = set.Tables[0].Rows[i]["Permission_Url"].ToString();
                item.Permission_AllAction = set.Tables[0].Rows[i]["Permission_AllAction"].ToString();
                if (set.Tables[0].Rows[i]["Permission_Layer"].ToString() != "") item.Permission_Layer = int.Parse(set.Tables[0].Rows[i]["Permission_Layer"].ToString());
                if (set.Tables[0].Rows[i]["Permission_ModelKeyId"].ToString() != "") item.Permission_ModelKeyId = int.Parse(set.Tables[0].Rows[i]["Permission_ModelKeyId"].ToString());
                this.Modellist.Add(item);
                this.GetModelList(Convert.ToInt32(set.Tables[0].Rows[i]["Permission_Id"]));
            }
            return this.Modellist;
        }

        public int GetPermissionIdByFlag(string Flag)
        {
            return SqlHelper.GetNoteCount("select Permission_Id from Dcms_Permission where Permission_Flag='" + Flag + "'");
        }

        public IList<Dcms_PermissionModel> GetUserPermissionModel(int ParentID)
        {
            string sQLString = "select * from Dcms_Permission where Permission_ParentID=@Permission_ParentID Order By Permission_Order Asc";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Permission_ParentID", SqlDbType.Int) };
            cmdParms[0].Value = ParentID;
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            return this.DataSetToList(ds);
        }

        public IList<Dcms_PermissionModel> GetUserPermissionModelByRoleId(int ParentID, string pIdstr)
        {
            string sQLString = "select * from Dcms_Permission where Permission_ParentID=@Permission_ParentID And Permission_Id In (" + pIdstr + ") Order By Permission_Order Asc";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Permission_ParentID", SqlDbType.Int) };
            cmdParms[0].Value = ParentID;
            DataSet ds = SqlHelper.CreateDataSet(sQLString, cmdParms);
            return this.DataSetToList(ds);
        }

        public Dcms_PermissionModel Update(Dcms_PermissionModel model)
        {
            string sQLString = "update Dcms_Permission set Permission_Name=@Permission_Name,Permission_Flag=@Permission_Flag,Permission_ParentID=@Permission_ParentID,Permission_Order=@Permission_Order,Permission_Url=@Permission_Url,Permission_AllAction=@Permission_AllAction,Permission_Layer=@Permission_Layer,Permission_ModelKeyId=@Permission_ModelKeyId where Permission_Id=@Permission_Id";
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Permission_Name", SqlDbType.VarChar), new SqlParameter("@Permission_Flag", SqlDbType.VarChar), new SqlParameter("@Permission_ParentID", SqlDbType.Int), new SqlParameter("@Permission_Order", SqlDbType.Int), new SqlParameter("@Permission_Url", SqlDbType.VarChar), new SqlParameter("@Permission_AllAction", SqlDbType.VarChar), new SqlParameter("@Permission_Layer", SqlDbType.Int), new SqlParameter("@Permission_ModelKeyId", SqlDbType.SmallInt), new SqlParameter("@Permission_Id", SqlDbType.Int) };
            cmdParms[0].Value = model.Permission_Name;
            cmdParms[1].Value = model.Permission_Flag;
            cmdParms[2].Value = model.Permission_ParentID;
            cmdParms[3].Value = model.Permission_Order;
            cmdParms[4].Value = model.Permission_Url;
            cmdParms[5].Value = model.Permission_AllAction;
            cmdParms[6].Value = model.Permission_Layer;
            cmdParms[7].Value = model.Permission_ModelKeyId;
            cmdParms[8].Value = model.Permission_Id;
            SqlHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }
    }
}

